package com.huazai.util;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @description: 邮件发工具类
 * @author: pinnuli
 * @date: 2018-4-25
 */

public class MailUtil implements Runnable {

    /**
     * 收件人邮箱
     */
    private String email;

    /**
     * 激活码
     */
    private String code;

    public MailUtil(String email, String code) {
        this.email = email;
        this.code = code;
    }

    @Override
    public void run() {
        // 1.创建连接对象javax.mail.Session
        // 2.创建邮件对象 javax.mail.Message
        // 3.发送一封激活邮件

        // 发件人电子邮箱
        String from = "2963095996@qq.com";
        // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)
        String host = "smtp.qq.com";

        // 获取系统属性,设置邮件服务器,打开认证
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", host);
        properties.setProperty("mail.smtp.auth", "true");

        try {
            //QQ邮箱需要下面这段代码，163邮箱不需要
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);

//            "pcoamoqqxkepdfae"
            // 1.获取默认session对象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("2963095996@qq.com", "mslcsdfsepuzdghc");
                }
            });

            // 2.创建邮件对象
            Message message = new MimeMessage(session);
            // 2.1设置发件人
            message.setFrom(new InternetAddress(from));
            // 2.2设置接收人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
            // 2.3设置邮件主题
            message.setSubject("账号激活");
            // 2.4设置邮件内容
            String content = "<!DOCTYPE html>\n" +
                    "<html lang=\"en\" xmlns:th=\"http://www.thymeleaf.org\">\n" +
                    "\n" +
                    "    <head>\n" +
                    "        <meta charset=\"UTF-8\">\n" +
                    "        <title>激活邮件</title>\n" +
                    "        <style type=\"text/css\">\n" +
                    "            * {\n" +
                    "                margin: 0;\n" +
                    "                padding: 0;\n" +
                    "                box-sizing: border-box;\n" +
                    "                font-family: Arial, Helvetica, sans-serif;\n" +
                    "            }\n" +
                    "\n" +
                    "            body {\n" +
                    "                background-color: #ECECEC;\n" +
                    "            }\n" +
                    "\n" +
                    "            .container {\n" +
                    "                width: 800px;\n" +
                    "                margin: 50px auto;\n" +
                    "            }\n" +
                    "\n" +
                    "            .header {\n" +
                    "                height: 80px;\n" +
                    "                background-color: #49bcff;\n" +
                    "                border-top-left-radius: 5px;\n" +
                    "                border-top-right-radius: 5px;\n" +
                    "                padding-left: 30px;\n" +
                    "            }\n" +
                    "\n" +
                    "            .header h2 {\n" +
                    "                padding-top: 25px;\n" +
                    "                color: white;\n" +
                    "            }\n" +
                    "\n" +
                    "            .content {\n" +
                    "                background-color: #fff;\n" +
                    "                padding-left: 30px;\n" +
                    "                padding-bottom: 30px;\n" +
                    "                border-bottom: 1px solid #ccc;\n" +
                    "            }\n" +
                    "\n" +
                    "            .content h2 {\n" +
                    "                padding-top: 20px;\n" +
                    "                padding-bottom: 20px;\n" +
                    "            }\n" +
                    "\n" +
                    "            .content p {\n" +
                    "                padding-top: 10px;\n" +
                    "            }\n" +
                    "\n" +
                    "            .footer {\n" +
                    "                background-color: #fff;\n" +
                    "                border-bottom-left-radius: 5px;\n" +
                    "                border-bottom-right-radius: 5px;\n" +
                    "                padding: 35px;\n" +
                    "            }\n" +
                    "\n" +
                    "            .footer p {\n" +
                    "                color: #747474;\n" +
                    "                padding-top: 10px;\n" +
                    "            }\n" +
                    "        </style>\n" +
                    "    </head>\n" +
                    "\n" +
                    "    <body>\n" +
                    "        <div class=\"container\">\n" +
                    "            <div class=\"header\">\n" +
                    "                <h2>欢迎加入图书管理系统! （在线图书管理平台）</h2>\n" +
                    "            </div>\n" +
                    "            <div class=\"content\">\n" +
                    "                <h2>亲爱的用户您好</h2>\n" +
                    "<h2>这是一封激活邮件,激活请点击以下链接</h2><h3><a href='http://42.193.113.124:8023/user/active?code=" + code + "'>http://42.193.113.124:8023/user/active?code=" + code + "</a></h3>" +
                    "                <p>当您在使用本网站时，务必要遵守法律法规</p>\n" +
                    "                <p>如果您有什么疑问可以联系管理员，Email: <b>2963095996@qq.com</b></p>\n" +
                    "            </div>\n" +
                    "            <div class=\"footer\">\n" +
                    "                <p>此为系统邮件，请勿回复</p>\n" +
                    "                <p>请保管好您的信息，避免被他人盗用</p>\n" +
                    "                <p>©HUAZAI</p>\n" +
                    "            </div>\n" +
                    "        </div>\n" +
                    "    </body>\n" +
                    "\n" +
                    "</html>\n";
            message.setContent(content, "text/html;charset=UTF-8");
            // 3.发送邮件
            Transport.send(message);
            System.out.println("邮件成功发送!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
